package com.cxd.web.frontend;

import com.cxd.entity.EbUser;
import com.cxd.service.IEbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/f-login")
public class EbLoginController {
    @Autowired
    private IEbUserService iEbUserService;
    //登录
    @RequestMapping("/to_login")
    public String toLogin(){
        return "login";//login.jsp
    }
    @RequestMapping("/login")
    public  String  login(HttpServletRequest request, String euUserId, String euPassword){
        System.out.println("username=="+euUserId+"password=="+euPassword);
        //调用service的iEbservice的登陆方法（根据用户和密码查询是否存在这个用户）
        EbUser lgnUser=iEbUserService.login(euUserId,euPassword);
        System.out.println("lgnUser=="+lgnUser);
        HttpSession session=request.getSession();
        if(lgnUser==null){
            //保存一个登陆失败的标记
            session.setAttribute("lgn_error","lgn_error");
            //重新登录
            return "redirect:/f-login/to_login";//重定向
        }
        //将用户放到session
        session.setAttribute("lgnUser",lgnUser);
        //清除错误提示
        session.removeAttribute("lgn_error");
//        return "redirect:f-index/index";
        if(lgnUser.getEuStatus()==2){
            return "redirect:/index/manage";
        }
        return "redirect:/f-index/index";
    }
}
